﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public enum UILayer
{
    Bottom=0,
    Mid=1,
    Pop=2,
    Top=3
}
public enum UIPanel
{
    StartPanel,
    LoginPanel,
    LobbyPanel,
    PlayerInfoBox,
    FunctionInfoBox,
    ShopPanel,
    DetailBox
}
public class UIMgr : MonoSingleton<UIMgr>, IManager
{
    private readonly Dictionary<UIPanel, KeyValuePair<string, UILayer>> uiInfo = new Dictionary<UIPanel, KeyValuePair<string, UILayer>>()
    {

    };

    private Dictionary<UILayer, Transform> layer2parent;
    private Dictionary<string,UIBase> openedUI;
    private Image blockImg;
    private TipLabels tipRoot;

    public bool CanTouch
    {
        get => !blockImg.raycastTarget;
        set => blockImg.raycastTarget = !value;
    }

    public void Init()
    {
        Loader.Instance.LoadAsyn<Transform>("Prefab/UIRoot", (loadTrs) =>
        {
            Transform uiRoot = Instantiate<Transform>(loadTrs);
            uiRoot.SetParent(transform);
            layer2parent = new Dictionary<UILayer, Transform>()
            {
                {UILayer.Bottom,uiRoot.Find("Canvas/Bottom") },
                {UILayer.Mid,uiRoot.Find("Canvas/Mid") },
                {UILayer.Pop,uiRoot.Find("Canvas/Pop") },
                {UILayer.Top,uiRoot.Find("Canvas/Top") }
            };
            openedUI = new Dictionary<string, UIBase>();
            blockImg = uiRoot.Find("Canvas/Top/BlockImg").GetComponent<Image>();
            blockImg.raycastTarget = false;
            tipRoot = uiRoot.Find("Canvas/Top/TipLabel").RequireComponent<TipLabels>();
            OpenUI<StartPanel>(UIPanel.StartPanel);
        });
    }

    public void UnInit()
    {
        
    }

    public void OpenUI<T>(UIPanel openPanel,UIPanel closePanel,params System.Object[] parameters)where T:UIBase
    {
        UILayer layer = uiInfo[openPanel].Value;
        Transform parent = layer2parent[layer];
        string resourcePath = uiInfo[openPanel].Key;
        if (openedUI.ContainsKey(resourcePath))
        {
            Debug.LogWarningFormat("已经打开过面板了，如需重新打开，请先关闭：{0}", resourcePath);
            return;
        }
        Loader.Instance.LoadAsyn<Transform>(resourcePath, (loadTrs) =>
        {
            Transform spawnTrs = SpawnUI(loadTrs, parent);
            var uiBase = spawnTrs.RequireComponent<T>();
            uiBase.Open(parameters);
            string closePath = uiInfo[closePanel].Key;
            UIBase closeUI = openedUI[closePath];
            CloseUI(closeUI, closePanel);
            openedUI.Add(resourcePath, uiBase);
        });
    }

    public void OpenUI<T>(UIPanel panel, params System.Object[] parameters) where T : UIBase
    {
        UILayer layer = uiInfo[panel].Value;
        Transform parent = layer2parent[layer];
        string resourcePath = uiInfo[panel].Key;
        if (openedUI.ContainsKey(resourcePath))
        {
            Debug.LogWarningFormat("已经打开过面板了，如需重新打开，请先关闭：{0}", resourcePath);
            return;
        }
        Loader.Instance.LoadAsyn<Transform>(resourcePath, (loadTrs) =>
        {
            Transform spawnTrs = SpawnUI(loadTrs, parent);
            var uiBase = spawnTrs.RequireComponent<T>();
            uiBase.Open(parameters);
            openedUI.Add(resourcePath, uiBase);
        });
    }

    public void CloseUI(UIBase ui,UIPanel panel)
    {
        string resourcePath = uiInfo[panel].Key;
        if (!openedUI.ContainsKey(resourcePath))
        {
            Debug.LogWarningFormat("打开缓存中不存在该面板：{0}", resourcePath);
            return;
        }
        ui.Close();
        openedUI.Remove(resourcePath);
    }

    public bool IsOpen(string resourcePath)
    {
        return openedUI.ContainsKey(resourcePath);
    }

    private Transform SpawnUI(Transform loadTrs,Transform parent)
    {
        Transform spawnTrs = Instantiate<Transform>(loadTrs);
        spawnTrs.SetParent(parent);
        spawnTrs.GetComponent<RectTransform>().Reset();
        return spawnTrs;
    }

    public void ShowTipLabel(string label)
    {
        tipRoot.ShowTip(label);
    }
}

public struct UIOpenData
{
    public string resourcePath;
    public UILayer layer;
    public Action callback;

    public UIOpenData(string path,UILayer layer,Action callback)
    {
        this.resourcePath = path;
        this.layer = layer;
        this.callback = callback;
    }
}
